<template>
  <div class="app-container">
    <el-form ref="transaction" :model="transaction" :rules="transactionRules" label-width="80px" >
      <el-form-item label="债券代码" prop="debentureCode">
        <el-col :span="11">
          <el-input v-model="transaction.debentureCode" @blur="codeInputBlur" placeholder="债券代码" style="width: 80%;"/>
        </el-col>

        <el-col :span="11">
          <el-form-item label="交易日期" prop="transactionDate">
            <el-date-picker v-model="transaction.transactionDate" type="date" value-format="yyyy-MM-dd" placeholder="选择日期" style="width: 80%;"/>
          </el-form-item>
        </el-col>

      </el-form-item>
      <el-form-item label="数量" prop="amount">
        <el-col :span="11">
          <el-input v-model="transaction.amount" placeholder="数量" @blur="amountInputBlur" style="width: 80%;"/>
        </el-col>
        <el-col :span="11">
          <el-form-item label="单价" prop="price">
            <el-input v-model="transaction.price" @blur="priceInputBlur" placeholder="单价" style="width: 80%;"/>
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item label="交易方向" prop="transactionDirection">
        <el-select v-model="transaction.transactionDirection" placeholder="请选择交易方向">
          <el-option label="卖出" value="1"/>
          <el-option label="买入" value="2"/>
        </el-select>
      </el-form-item>
      <el-form-item label="结算金额">
        <el-col :span="11">
          <el-input v-model="transaction.settlementAmount"  placeholder="结算金额" style="width: 80%;" :disabled="true"/>
        </el-col>
      </el-form-item>
      <el-form-item label="估值金额">
        <el-col :span="11">
          <el-input v-model="valuation"  placeholder="结算金额" style="width: 80%;" :disabled="true"/>
        </el-col>
      </el-form-item>
      <el-form-item class="de-button">
        <el-button type="primary" @click="onSubmit('transaction')">保存</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>


<script>
import transaction from '@/api/transaction'
import { getToken } from '@/utils/auth'
export default {
  data() {
    return {
      form: {
        name: '',
        region: '',
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: ''
      },
      transaction: {
        transactionAccount: '',
        debentureCode: '',
        transactionDate: '',
        amount: '',
        price: '',
        transactionDirection: '',
        settlementAmount: '',
      },
      transactionRules: {
        debentureCode: [{
          required: true,
          message: '请输入债券代码',
          trigger: 'blur'
        }],
        transactionDate: [{
          required: true,
          message: '请选择交易日期',
          trigger: 'blur'
        }],
        transactionDirection: [{
          required: true,
          message: '请选择交易方向',
          trigger: 'blur'
        }],
        amount: [{
          required: true,
          message: '请输入数量',
          trigger: 'blur'
        }],
        price: [{
          required: true,
          message: '请输入单价',
          trigger: 'blur'
        }]
      },
      popup: '',
      account: '',
      valuation: ''
    }
  },
  created() {
    console.log(getToken())
    this.getAccount()
    transaction.transactionDate = new Date()
  },


  methods: {
    getAccount() {
      this.account = sessionStorage.getItem('account')
      console.log(sessionStorage.getItem('account'))
      console.log('--------')
      console.log(this.account)
    },

    codeInputBlur(){
      this.searchValutionByCode();
    },
    priceInputBlur(){
      this.transaction.settlementAmount = this.transaction.amount*this.transaction.price;
      console.log(this.transaction.settlementAmount);
    },
    amountInputBlur(){
      this.transaction.settlementAmount = this.transaction.amount*this.transaction.price;
      console.log(this.transaction.settlementAmount);
    },

    searchValutionByCode(){
         var de = this.transaction.debentureCode;
          transaction.searchByCode(de).then(response => {
            this.valuation = response.data
          }).catch(error =>{
            this.$message({
              showClose: true,
              message: '找不到这个债券',
              type: 'error'
            })
          })
    },

    onSubmit(formName) {
      this.$refs[formName].validate((valid)=>{
        if(valid){
          transaction.save(this.transaction).then(response => {
            this.popup = response.data
            console.log(this.popup)
            this.check()
          })
        }
      })
        .catch(error => { // 请求失败
          console.log(error)
        })
    },
    check() {
      if (this.popup === 0) {
        this.$message({
          showClose: true,
          message: '恭喜你，交易成功',
          type: 'success'
        })
      }
      if (this.popup === 1) {
        this.open()
      }
    },
    open() {
      this.$alert('交易触发偏移度', '警告', {
        confirmButtonText: '确定'
      })
      this.$message({
        showClose: true,
        message: '恭喜你，交易成功',
        type: 'success'
      })
    },
    onCancel() {
      this.$message({
        message: 'cancel!',
        type: 'warning'
      })
    }
  }
}
</script>

<style scoped>
.right-form{
  width: 300px;
}
.left-form{
  width: 300px;
}
.app-container{
  color: white;
  margin-left: 120px;
}
.de-button{
  margin-top: 60px;
  margin-left: 300px;
}
</style>
